package com.gitee.cirnochat.group.controller;


import com.gitee.cirnochat.common.annotation.AuthCheck;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.group.model.vo.req.GroupCreateRequest;
import com.gitee.cirnochat.group.model.vo.resp.GroupVo;
import com.gitee.cirnochat.group.service.GroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "群组信息接口", description = "群组相关接口")
@RestController
@RequestMapping("/group")
@RequiredArgsConstructor
public class GroupController {
    private final GroupService groupService;

    @Operation(summary = "创建群组", description = "创建一个新的群组")
    @ApiResponse(responseCode = "200", description = "创建成功",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = GroupVo.class)))
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @PostMapping
    public Result<GroupVo> createGroup(@RequestPart("file") MultipartFile file,
                                       @RequestPart("data") GroupCreateRequest groupCreateRequest,
                                       HttpServletRequest request) {
        return Result.success(groupService.createGroup(groupCreateRequest, file, request));
    }

    @Operation(summary = "修改群组信息", description = "修改群组信息")
    @ApiResponse(responseCode = "200", description = "修改群组信息成功",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = GroupVo.class)))
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @PutMapping("/edit")
    public Result<GroupVo> editGroup(@RequestParam(required = true) Long id,
                                     @RequestParam(required = true) String groupName,
                                     @RequestParam(required = true) String groupType,
                                     @RequestPart("file") MultipartFile file,
                                     HttpServletRequest request) {
        GroupCreateRequest groupCreateRequest = new GroupCreateRequest();
        groupCreateRequest.setOwnerId(id);
        groupCreateRequest.setGroupName(groupName);
        groupCreateRequest.setGroupType(groupType);

        return Result.success(groupService.createGroup(groupCreateRequest, file, request));
    }


    @Operation(summary = "转让群组", description = "转让群组")
    @ApiResponse(responseCode = "200", description = "转让成功",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = GroupVo.class)))
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @PutMapping("/transfer")
    public Result<GroupVo> transferGroup(@RequestParam(required = true) Long id,
                                         @RequestParam(required = true) String groupName,
                                         @RequestParam(required = true) String groupType,
                                         @RequestPart("file") MultipartFile file,
                                         HttpServletRequest request) {
        GroupCreateRequest groupCreateRequest = new GroupCreateRequest();
        groupCreateRequest.setOwnerId(id);
        groupCreateRequest.setGroupName(groupName);
        groupCreateRequest.setGroupType(groupType);

        return Result.success(groupService.createGroup(groupCreateRequest, file, request));
    }


    @Operation(summary = "解散群组", description = "解散群组")
    @ApiResponse(responseCode = "200", description = "创建成功",
            content = @Content(mediaType = "application/json", schema = @Schema(implementation = GroupVo.class)))
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @DeleteMapping
    public Result<Void> deleteGroup(@RequestParam(required = true) Long id,
                                    @RequestParam(required = true) String groupName,
                                    @RequestParam(required = true) String groupType,
                                    @RequestPart("file") MultipartFile file,
                                    HttpServletRequest request) {
        GroupCreateRequest groupCreateRequest = new GroupCreateRequest();
        groupCreateRequest.setOwnerId(id);
        groupCreateRequest.setGroupName(groupName);
        groupCreateRequest.setGroupType(groupType);

        return Result.success();
    }


//    /**
//     * 上传图片作为头像
//     *
//     * @param file      上传文件
//     * @param request           请求
//     * @return {@link Result}<{@link ImageVo}>
//     */
//    @PostMapping("/upload/{groupId}")
//    @Operation(summary = "上传群聊头像", description = "上传图片作为群聊头像，需要群聊已经存在")
//    @ApiResponse(
//            responseCode = "200",
//            description = "成功",
//            content = @Content(
//                    mediaType = "application/json",
//                    schema = @Schema(implementation = ImageVo.class)
//            )
//    )
//    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
//    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
//    public Result<ImageVo> uploadGroupAvatar(@Parameter(name = "groupId" , description = "群聊ID" , in = ParameterIn.PATH) @PathVariable Long groupId , @RequestPart(required = true) MultipartFile file, HttpServletRequest request) {
//
//        return Result.success();
//    }
}
